package com.hbk.codemaker;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.hbk.programe.Method;

public class MethodPanel extends JPanel implements MouseListener{
	
	public static final int METHOD_HEIGHT=200;
	
	private MainFrame window;
	
	private JLabel methodLabel=new JLabel("请求");
	
	private JTextField methodField=new JTextField();
	
	private JLabel uriLabel=new JLabel("URI");
	
	private JTextField uriField=new JTextField();
	
	private JLabel resultLabel=new JLabel("结果");
	
	private JTextField resultField=new JTextField();
	
	private JLabel nameLabel=new JLabel("方法");
	
	private JTextField nameField=new JTextField();
	
	private JLabel algorithmtLabel=new JLabel("算法");
	
	private JTextField algorithmField=new JTextField();
	
	private JLabel cloumnsLabel=new JLabel("行");
	
	private JTextArea cloumnsField=new JTextArea();
	
	public MethodPanel(int width,MainFrame window) {
		this.window=window;
		setLayout(null);
		
		methodLabel.setBounds(0, 0, 30, 32);
		methodField.setBounds(30+10, 0, 150, 32);
		uriLabel.setBounds(30+10+150+10, 0, 30, 32);
		uriField.setBounds(30+10+150+10+30+10, 0, 150, 32);
		
		resultLabel.setBounds(30+10+150+10+30+10+150+10, 0, 30, 32);
		resultField.setBounds(30+10+150+10+30+10+150+10+30+10, 0, 150, 32);
		nameLabel.setBounds(30+10+150+10+30+10+150+10+30+10+150+10, 0, 30, 32);
		nameField.setBounds(30+10+150+10+30+10+150+10+30+10+150+10+30+10, 0, 150, 32);
		
		algorithmtLabel.setBounds(0, 40, 30, 32);
		algorithmField.setBounds(30+10, 40, 150, 32);
		cloumnsLabel.setBounds(30+10+150+10, 40, 30, 32);
		
		JPanel panelOutput= new JPanel();
		panelOutput.setLayout(new BorderLayout());
	    panelOutput.setBounds(30+10+150+10+30+10, 40, 550, 160);
	    panelOutput.add(new JScrollPane(cloumnsField),BorderLayout.CENTER);
		
		cloumnsField.addMouseListener(this);
		addMouseListener(this);
		
		add(methodLabel);
		add(methodField);
		add(uriLabel);
		add(uriField);
		
		add(resultLabel);
		add(resultField);
		add(nameLabel);
		add(nameField);
		
		add(algorithmtLabel);
		add(algorithmField);
		add(cloumnsLabel);
		add(panelOutput);
		
		setPreferredSize(new Dimension(width, METHOD_HEIGHT));
		setBackground(Color.WHITE);
	}
	
	public void setMethod(Method method) {
		methodField.setText(method.getMethod());
		uriField.setText(method.getUri());
		resultField.setText(method.getResult());
		nameField.setText(method.getName());
		algorithmField.setText(method.getAlgorithm());
		StringBuffer text=new StringBuffer();
		for(String column:method.getColumns()) {
			text.append(column).append("\r\n");
		}
		cloumnsField.setText(text.toString());
	}
	
	public void repaint(int width) {
		setPreferredSize(new Dimension(width, METHOD_HEIGHT));
		repaint();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		System.out.println("gained");
		e.getComponent().setBackground(new Color(151,227,235));
		if(e.getComponent() instanceof JTextArea) {
			window.setLastFocusComponent(e.getComponent());
		}else {
			window.setLastFocusComponent(this);
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		System.out.println("lost");
		e.getComponent().setBackground(new Color(255,255,255));
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	

}
